/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package com.oop.example.gui;

import com.oop.example.work.bel.Utils;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Properties;
import javax.swing.JOptionPane;

/**
 *
 * @author -
 */
public class LangsDialog extends javax.swing.JDialog {

    /**
     * Creates new form LangsDialog
     */
    public LangsDialog(Window parent) {
        super(parent, ModalityType.APPLICATION_MODAL);
        initComponents();
        update();
        jButtonSave.addActionListener(new SaveActionListener());
    }

    private void update() {
        Properties props = Utils.getLangs();
        List<String> list = new ArrayList<String>();
        for (String str : props.stringPropertyNames()) {
            list.add(str);
        }
        Collections.sort(list);
        String d = "";
        for (String s : list) {
            d += s + Utils.NL;
        }
        jTextPaneLangs.setText(d);
    }

    private class SaveActionListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {

            Properties props = new Properties();

            try {
                String text = jTextPaneLangs.getText();
                String[] lines = text.split(Utils.NL);
                if (lines.length == 0) {
                    JOptionPane.showMessageDialog(LangsDialog.this, "Не найдены данные для загрузки");
                    return;
                }
                for (String line : lines) {
                    props.setProperty(line, line);

                }
                props.storeToXML(new FileOutputStream(new File(Utils.LangsFile)), "", Utils.LangsFileEncoding);
                JOptionPane.showMessageDialog(LangsDialog.this, "В справочнике " + props.size() + " значение(й)");
            } catch (Exception ex) {
                ex.printStackTrace(System.out);
            }
            update();
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTextPaneLangs = new javax.swing.JTextPane();
        jButtonSave = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Языки");

        jScrollPane1.setViewportView(jTextPaneLangs);

        jButtonSave.setText("Сохранить");
        jButtonSave.setToolTipText("");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 293, Short.MAX_VALUE)
                        .addComponent(jButtonSave)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 411, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonSave)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonSave;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextPane jTextPaneLangs;
    // End of variables declaration//GEN-END:variables
}
